package ar.com.personalTravellers.rowProcessors;

import ar.com.personalTravellers.beans.FlexInternationalizationKeyValue;

import com.angel.common.helpers.StringHelper;
import com.angel.io.exceptions.InvalidRowDataException;
import com.angel.io.parameters.ParametersService;
import com.angel.io.processors.commands.exports.ExportRowProcessorCommand;
import com.angel.io.processors.commands.imports.ImportRowProcessorCommand;
import com.angel.io.type.rows.ExportRowFile;
import com.angel.io.type.rows.ImportRowFile;
import com.angel.io.type.rows.impl.HeaderRowFile;

/**
 *
 * @author William
 */
public class FlexInternationalizationRowProcessorCommand implements ImportRowProcessorCommand, ExportRowProcessorCommand {

    public static final String KEY_COLUMN = "key";
	public static final String SPANISH_VALUE_COLUMN = "spanish";
    public static final String FRENCH_VALUE_COLUMN = "french";
    public static final String ENGLISH_VALUE_COLUMN = "english";
    public static final String GERMAN_VALUE_COLUMN = "german";
    public static final String CHECO_VALUE_COLUMN = "checo";
    public static final String PORTUGUES_VALUE_COLUMN = "portugues";

    private String currentKey;

    public void checkRowData(ImportRowFile importRowFile, HeaderRowFile headerRowFile) throws InvalidRowDataException {
        String key = importRowFile.getStringValue(headerRowFile.getColumnPosition(KEY_COLUMN));
		String spanishValue = importRowFile.getStringValue(headerRowFile.getColumnPosition(SPANISH_VALUE_COLUMN));
        String englishValue = importRowFile.getStringValue(headerRowFile.getColumnPosition(ENGLISH_VALUE_COLUMN));
        String frenchValue = importRowFile.getStringValue(headerRowFile.getColumnPosition(FRENCH_VALUE_COLUMN));
        String germanValue = importRowFile.getStringValue(headerRowFile.getColumnPosition(GERMAN_VALUE_COLUMN));
        String portuguesValue = importRowFile.getStringValue(headerRowFile.getColumnPosition(PORTUGUES_VALUE_COLUMN));
        String checoValue = importRowFile.getStringValue(headerRowFile.getColumnPosition(CHECO_VALUE_COLUMN));

        Boolean areAllNotEmpties = StringHelper.areAllNotEmpty(key, spanishValue, englishValue);
		if( !areAllNotEmpties ){
			throw new InvalidRowDataException("Some row data are NULL - " +
					"key: [" + key + "] - " +
                    "spanishValue: [" + spanishValue + "] - " +
                    "englishValue: [" + englishValue + "] - " +
                    "frenchValue: [" + frenchValue + "] - " +
                    "germanValue: [" + germanValue + "] - " +
                    "checoValue: [" + checoValue + "] - " + 
                    "portuguesValue: [" + portuguesValue + "].");
		}
    }

    public Object processRow(ImportRowFile importRowFile, HeaderRowFile headerRowFile, ParametersService parametersService) {
        String key = importRowFile.getStringValue(headerRowFile.getColumnPosition(KEY_COLUMN));
		String spanishValue = importRowFile.getStringValue(headerRowFile.getColumnPosition(SPANISH_VALUE_COLUMN));
        String englishValue = importRowFile.getStringValue(headerRowFile.getColumnPosition(ENGLISH_VALUE_COLUMN));
        String frenchValue = importRowFile.getStringValue(headerRowFile.getColumnPosition(FRENCH_VALUE_COLUMN));
        String germanValue = importRowFile.getStringValue(headerRowFile.getColumnPosition(GERMAN_VALUE_COLUMN));
        String portuguesValue = importRowFile.getStringValue(headerRowFile.getColumnPosition(PORTUGUES_VALUE_COLUMN));
        String checoValue = importRowFile.getStringValue(headerRowFile.getColumnPosition(CHECO_VALUE_COLUMN));

        FlexInternationalizationKeyValue keyValue = new FlexInternationalizationKeyValue(key);
        keyValue.setEnglishValue(englishValue);
        keyValue.setSpanishValue(spanishValue);
        keyValue.setFrenchValue(frenchValue);
        keyValue.setGermanValue(germanValue);
        keyValue.setPortuguesValue(portuguesValue);
        keyValue.setChecoValue(checoValue);
        return keyValue;
    }

    public String[] getHeaderColumnsNames(){
        return new String[] {
                KEY_COLUMN,
                SPANISH_VALUE_COLUMN,
                ENGLISH_VALUE_COLUMN,
                FRENCH_VALUE_COLUMN,
                GERMAN_VALUE_COLUMN,
                PORTUGUES_VALUE_COLUMN
           };
    }

    public void processRow(ExportRowFile exportRowFile, Object o, HeaderRowFile headerRowFile) throws InvalidRowDataException {
        FlexInternationalizationKeyValue keyValue = (FlexInternationalizationKeyValue) o;
        exportRowFile.addStringValue(keyValue.getKey(), headerRowFile.getColumnPosition(KEY_COLUMN));

        if(this.getCurrentKey().equals(SPANISH_VALUE_COLUMN)){
            exportRowFile.addStringValue(keyValue.getSpanishValue(), headerRowFile.getColumnPosition(SPANISH_VALUE_COLUMN));
        }
        if(this.getCurrentKey().equals(ENGLISH_VALUE_COLUMN)){
            exportRowFile.addStringValue(keyValue.getEnglishValue(), headerRowFile.getColumnPosition(ENGLISH_VALUE_COLUMN));
        }
        if(this.getCurrentKey().equals(FRENCH_VALUE_COLUMN)){
            exportRowFile.addStringValue(keyValue.getFrenchValue(), headerRowFile.getColumnPosition(FRENCH_VALUE_COLUMN));
        }
        if(this.getCurrentKey().equals(GERMAN_VALUE_COLUMN)){
            exportRowFile.addStringValue(keyValue.getGermanValue(), headerRowFile.getColumnPosition(GERMAN_VALUE_COLUMN));
        }
        if(this.getCurrentKey().equals(PORTUGUES_VALUE_COLUMN)){
            exportRowFile.addStringValue(keyValue.getPortuguesValue(), headerRowFile.getColumnPosition(PORTUGUES_VALUE_COLUMN));
        }

    }

    public void checkHeader(HeaderRowFile headerRowFile) throws InvalidRowDataException {
        /*if(!headerRowFile.hasAllColumnName(this.getHeaderColumnsNames())){
			throw new InvalidRowDataException("Some row data are NULL.");
		}*/
    }

    public String getCurrentKey() {
        return currentKey;
    }

    public void setCurrentKey(String currentKey) {
        this.currentKey = currentKey;
    }
}